/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-07-28 11:31:11
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-28 15:22:06
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import { LazyLoadEvent,TreeNode } from 'primeng/api';
import { BomContentServiceProxy ,BomDetaillist} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
@Component({
    selector: 'bomLook',
    templateUrl: './bom-look.component.html',
    styleUrls: ['./bom-look.component.less']
})
export class BomLookComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    saving = false;
    active = false;
    loadingtree: boolean;
    ischeckTree: any;//当前点击节点
    files2: any;
    files1: any;
    loadingTable: boolean;
    constructor(
        injector: Injector,
        private _bomContentServiceProxy: BomContentServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    //打开弹窗
    onShown(): void {
        this.getTreeInfo();
    }
    //查看新增任务
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.files1 = [];
        this.files2 = [];
        this.active = false;
        this.modal.hide();
    }
    nodeSelect(event) {
        if (event.node.id != this.editData.id ) {
            this.getTreedata(event.node.id);
        }
    }
    getTreedata(row) {
        this.loadingTable = true;
        this.files1 = [];
        this.files1[0] = new BomDetaillist();
        this._bomContentServiceProxy.getBomChildTreeGrid(
            row,
            undefined
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                // this.files1[0].data = result.data;
                // this.files1[0].children = result.children;
                let rs = [];
                let item = { data: undefined, children: [] };
                item.data = result.data;
                item.children = result.children;
                rs.push(item);
                this.loadingTable = false;
                this.expandAll(rs);
                this.files1 = rs;
            });
    }
    expandAll(rs) {
        for (const node of rs) {
            this.TreeExpandRecursive(node, true);
        }
    }
    private TreeExpandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.TreeExpandRecursive(childNode, isExpand);
            });
        } 
    }
    getTreeInfo(): void {
        this.loadingtree = true;
        let id = 'FA568B8C-4EB5-47B1-92B1-F5AAFF234DE4'
        this._bomContentServiceProxy.getTreeGrid(
            this.editData.id
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                //建立父级节点
                this.files2 = [{
                    id: this.editData.id,
                    materialName: this.editData.materialName,
                    levelNum: this.editData.levelNum,
                    children: result ? result : []
                }];
                this.ischeckTree = {};
                this.loadingtree = false;
                this.collapseAll();
            });
    }
    collapseAll(){
        this.files2.forEach( node => {
            this.expandRecursive(node, true);
        } );
    }
    
    private expandRecursive(node:TreeNode, isExpand:boolean){
        node.expanded = isExpand;
        if (node.children){
            node.children.forEach( childNode => {
                this.expandRecursive(childNode, isExpand);
            } );
        }
    }
}
